# -*-* encoding:UTF-8 -*-
# author : mengy
# date   : 2018/11/22

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

sender = "1215012319@qq.com"
password = "dishang0117/"

email_host = "smtp.qq.com"
email_port = 25

receivers = ["1215012319@qq.com","mengyanyuan@outlook.com"]

message = MIMEMultipart()
message['From'] = Header("python 测试邮件","utf-8")
message['To'] = Header("接收者","utf-8")
message['Subject'] = Header("python 测试邮件","utf-8")

message.attach(MIMEText("这是一封带附件的测试邮件","plain","utf-8"))

# 构造附件1
file = open("mail_body.html","rb")
attach1 = MIMEText(file.read(),"base64","utf-8")
attach1['Content-Type'] = "application/octet-stream"
# 这里的filename可以任意写，写什么名字，邮件中显示什么名字
filename = "attachment;filename='%s'"% file.name
print filename
attach1['Content-Disposition'] = filename
message.attach(attach1)
file.close()
del file

# 构造附件2
file = open("text.txt","rb")
attach2 = MIMEText(file.read(),"base64","utf-8")
attach2['Content-Type'] = "application/octet-stream"
filename = "attachment;filename='%s'"% file.name
print filename
attach2['Content-Disposition'] = filename
message.attach(attach2)

try:
    emailObj = smtplib.SMTP()
    emailObj.connect(email_host,email_port)
    emailObj.login(sender,password)
    emailObj.sendmail(sender,receivers,message.as_string())
    print "邮件发送成功"
except Exception,e:
    print "邮件发送失败:",e